import { FakeApi } from '@wowon/utils';
import { getCurrentUser } from '../../auth-center';

const initData = [
  {
    id: 'r0',
    pid: '_root',
    rootid: '_root',
    name: 'wowon-admin',
    path: 'http://localhost:8299/wowon',
    redirect: '',
    type: 'dir',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
    remark: '系统菜单',
  },
  {
    id: 'r0m0',
    pid: 'r0',
    rootid: 'r0',
    path: '/',
    redirect: '/dashboard/home',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m1',
    pid: 'r0',
    rootid: 'r0',
    name: '首页',
    path: '/dashboard',
    type: 'dir',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m100',
    pid: 'r0m1',
    rootid: 'r0',
    path: '/dashboard',
    redirect: '/dashboard/home',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m101',
    pid: 'r0m1',
    rootid: 'r0',
    name: '系统概览',
    path: '/dashboard/home',
    component: '/dashboard/home',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m102',
    pid: 'r0m1',
    rootid: 'r0',
    name: '工作台',
    path: '/dashboard/workplace',
    component: '/dashboard/workplace',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m103',
    pid: 'r0m1',
    rootid: 'r0',
    name: '统计分析',
    path: '/dashboard/analysis',
    component: '/dashboard/analysis',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m104',
    pid: 'r0m1',
    rootid: 'r0',
    name: 'Ai 对话',
    path: '/dashboard/ai',
    component: '/dashboard/ai',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m2',
    pid: 'r0',
    rootid: 'r0',
    name: '权限管理',
    path: '/access',
    type: 'dir',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m200',
    pid: 'r0m2',
    rootid: 'r0',
    path: '/access',
    redirect: '/access/user',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m201',
    pid: 'r0m2',
    rootid: 'r0',
    name: '用户管理',
    path: '/access/user',
    component: '/access/user',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m20101',
    pid: 'r0m201',
    rootid: 'r0',
    name: '新增',
    path: null,
    component: null,
    type: 'button',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m20102',
    pid: 'r0m201',
    rootid: 'r0',
    name: '编辑',
    path: null,
    component: null,
    type: 'button',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m20103',
    pid: 'r0m201',
    rootid: 'r0',
    name: '删除',
    path: null,
    component: null,
    type: 'button',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m202',
    pid: 'r0m2',
    rootid: 'r0',
    name: '角色管理',
    path: '/access/role',
    component: '/access/role',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m20201',
    pid: 'r0m202',
    rootid: 'r0',
    name: '新增',
    path: null,
    component: null,
    type: 'button',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m20202',
    pid: 'r0m202',
    rootid: 'r0',
    name: '编辑',
    path: null,
    component: null,
    type: 'button',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m20203',
    pid: 'r0m202',
    rootid: 'r0',
    name: '删除',
    path: null,
    component: null,
    type: 'button',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m203',
    pid: 'r0m2',
    rootid: 'r0',
    name: '菜单管理',
    path: '/access/menu',
    component: '/access/menu',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m3',
    pid: 'r0',
    rootid: 'r0',
    name: '系统管理',
    path: '/system',
    type: 'dir',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m300',
    pid: 'r0m3',
    rootid: 'r0',
    path: '/system',
    redirect: '/system/file',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m301',
    pid: 'r0m3',
    rootid: 'r0',
    name: '文件管理',
    path: '/system/file',
    component: '/system/file',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m302',
    pid: 'r0m3',
    rootid: 'r0',
    name: '通知公告',
    path: '/system/notice',
    component: '/system/notice',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m30201',
    pid: 'r0m3',
    rootid: 'r0',
    name: '公告发布',
    path: '/system/notice/update/:id',
    component: '/system/notice/update',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m303',
    pid: 'r0m3',
    rootid: 'r0',
    name: '更新公告',
    path: '/system/update',
    component: '/system/update',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m304',
    pid: 'r0m3',
    rootid: 'r0',
    name: '系统帮助',
    path: '/system/help',
    component: '/system/help',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m305',
    pid: 'r0m3',
    rootid: 'r0',
    name: '用户反馈',
    path: '/system/feedback',
    component: '/system/feedback',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m306',
    pid: 'r0m3',
    rootid: 'r0',
    name: '错误日志',
    path: '/system/error',
    component: '/system/error',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m4',
    pid: 'r0',
    rootid: 'r0',
    name: '基础设置',
    path: '/base',
    type: 'dir',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m400',
    pid: 'r0m4',
    rootid: 'r0',
    path: '/base',
    redirect: '/base/dict',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m401',
    pid: 'r0m4',
    rootid: 'r0',
    name: '数据字典',
    path: '/base/dict',
    component: '/base/dict',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m402',
    pid: 'r0m4',
    rootid: 'r0',
    name: '参数设置',
    path: '/base/param',
    component: '/base/param',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m403',
    pid: 'r0m4',
    rootid: 'r0',
    name: '组织架构',
    path: '/base/organization',
    component: '/base/organization',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m5',
    pid: 'r0',
    rootid: 'r0',
    name: '个人中心',
    path: '/account',
    type: 'dir',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m500',
    pid: 'r0m5',
    rootid: 'r0',
    path: '/account',
    redirect: '/account/center',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m501',
    pid: 'r0m5',
    rootid: 'r0',
    name: '个人信息',
    path: '/account/center',
    component: '/account/center',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r0m502',
    pid: 'r0m5',
    rootid: 'r0',
    name: '个人设置',
    path: '/account/setting',
    component: '/account/setting',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r1',
    pid: '_root',
    rootid: '_root',
    name: 'wowon-admin',
    path: '',
    redirect: '',
    type: 'dir',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
    remark: '系统底部菜单',
  },
  {
    id: 'r1m1',
    pid: 'r1',
    rootid: '_root',
    name: '',
    path: '',
    redirect: '',
    type: 'dir',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r1m101',
    pid: 'r1m1',
    rootid: '_root',
    name: '系统帮助',
    path: '/system/help',
    redirect: '',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r1m102',
    pid: 'r1m1',
    rootid: '_root',
    name: '更新公告',
    path: '/system/update',
    redirect: '',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
  {
    id: 'r2',
    pid: '_root',
    rootid: '_root',
    name: 'wowon-admin',
    path: '',
    redirect: '',
    type: 'dir',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
    remark: '系统头像菜单',
  },
  {
    id: 'r2m1',
    pid: 'r2',
    rootid: '_root',
    name: '个人中心',
    path: '/account/center',
    redirect: '',
    type: 'path',
    backendAccess: '',
    frontendAccess: '',
    sort: 0,
  },
];

export const menuList = new FakeApi(initData, {
  queryType: { id: 'is', rootid: 'is' },
  sort: (x, y) => {
    let num = x.sort - y.sort;
    if (num !== 0) return num;
    num = new Date(y.updateTime) - new Date(x.updateTime);
    if (num !== 0) return num;
  },
});

export default {
  'GET /mock/menu/query': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      // res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await menuList.query(req.query);
    res.json(result);
  },
  'POST /mock/menu/create': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await menuList.create(req.body);
    res.json(result);
  },
  'POST /mock/menu/update': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await menuList.update(req.body);
    res.json(result);
  },
  'POST /mock/menu/remove': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await menuList.remove(req.body);
    res.json(result);
  },
  'GET /mock/menu/list': async (req, res) => {
    const currentUser = getCurrentUser(req);
    if (!currentUser) {
      res.statusCode = 401;
      res.json({ code: '-1', message: '权限不足' });
      return;
    }
    const result = await menuList.list(req.query);
    res.json(result);
  },
};
